package com.atguigu.tingshu.album.config;

import jakarta.annotation.PostConstruct;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Log4j2
public class AlbumAndSearchCallBackConfig implements RabbitTemplate.ReturnsCallback {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init(){
        rabbitTemplate.setReturnsCallback(this::returnedMessage);
    }

    @Override
    public void returnedMessage(ReturnedMessage returnedMessage) {
        log.error("上下架发送消息失败，失败专辑id："
                + new String(returnedMessage.getMessage().getBody())
                +"，交换机为："+ returnedMessage.getExchange()
                +"，routingKey为："+returnedMessage.getRoutingKey());
    }
}
